﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using Fishing.Model;
using MySql.Data.MySqlClient;
using NLog;

namespace Fishing.Dal
{
    public class AdminRolePermissionDal
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();


        /// <summary>
        /// ConStr
        /// </summary>
        private static readonly string ConStr = ConfigurationManager.ConnectionStrings["_fishingdb"].ConnectionString;

        /// <summary>
        ///  Add
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InsertDate(admin_role_permission model)
        {

            var ret = false;

            const string sql = "insert into admin_role_permission values " +
                               "(@rolePermissionID,@roleID,@enumID)";

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            command.Parameters.Add("@rolePermissionID", MySqlDbType.VarChar, 10).Value = model.rolePermissionID;
            command.Parameters.Add("@roleID", MySqlDbType.VarChar, 10).Value = model.roleID;
            command.Parameters.Add("@enumID", MySqlDbType.VarChar, 10).Value = model.enumID;
            try
            {
                connection.Open();
                ret = Convert.ToBoolean(command.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("InsertDate ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();

            }
            return ret;
        }

        /// <summary>
        /// modify
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateDate(admin_role_permission model)
        {

            var ret = false;

            const string sql = "UPDATE admin_role_permission set enumID=@enumID where roleID=@roleID";

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            command.Parameters.Add("@roleID", MySqlDbType.VarChar, 10).Value = model.roleID;
            command.Parameters.Add("@enumID", MySqlDbType.VarChar, 10).Value = model.enumID;
            try
            {
                connection.Open();
                ret = Convert.ToBoolean(command.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("UpdateDate ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();

            }
            return ret;
        }

        /// <summary>
        /// delete
        /// </summary>
        /// <param name="rolePermissionID"></param>
        /// <returns></returns>
        public bool DelDate(string rolePermissionID)
        {

            var ret = false;

            const string sql =
                "delete FROM admin_role_permission where rolePermissionID=@rolePermissionID";

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            command.Parameters.Add("@rolePermissionID", MySqlDbType.VarChar, 10).Value = rolePermissionID;
            try
            {
                connection.Open();
                ret = Convert.ToBoolean(command.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("DelDate ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();

            }


            return ret;
        }

        /// <summary>
        /// if exists
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public bool IsExists(string roleID)
        {

            var ret = false;

            const string sql =
                "select IFNULL(COUNT(1),0) from admin_role_permission where roleID = @roleID";

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            command.Parameters.Add("@roleID", MySqlDbType.VarChar, 10).Value = roleID;
            try
            {
                connection.Open();
                ret = Convert.ToBoolean(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("IsExists ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();

            }


            return ret;
        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<admin_role_permission> GetDataList(string where)
        {
            var retList = new List<admin_role_permission>();
            var sql = "select * from admin_role_permission where 1=1 ";
            if (where.Length > 0)
            {
                sql = sql + where;
            }

            sql = sql + " order by `rolePermissionID` asc";

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            try
            {
                connection.Open();
                using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            retList.Add(new admin_role_permission()
                            {
                                rolePermissionID = reader["rolePermissionID"].ToString(),
                                roleID = reader["roleID"].ToString(),
                                enumID = reader["enumID"].ToString()
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("GetDataList ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();
            }
            return retList;

        }
    }
}